package lec15dfs;

import java.util.Scanner;

public class H {
    static int n = 8;
    static int[] a = new int[n];
    static boolean[] book = new boolean[n];

    static void dfs(int k) {
        if (k == n) {
            // (0,a0) (1,a1).....(7,a7)
            boolean ok = true;
            for (int i = 0; i < n; i++)
                for (int j = i + 1; j < n; j++) {
                    // Qi(i,ai) Qj(j aj)
                    int dx = i - j;
                    int dy = a[i] - a[j];
                    if (dx == dy || dx + dy == 0) {
                        ok = false;
                    }
                }
            if (ok) {
                for (int i = 0; i < n; i++)
                    System.out.print(a[i]);
                System.out.println();
            }
            return;// 1
        }
        for (int i = 0; i < n; i++) {
            if (book[i])
                continue;
            a[k] = i;
            book[i] = true;
            dfs(k + 1);
            book[i] = false;
        }
    }

    public static void main(String[] args) {
        dfs(0);
    }

    static Scanner cin = new Scanner(System.in);
}